﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEX.Service.DataContracts;

namespace CLEX.Client.Metro.ViewModel.Home
{
    public class ChangePasswordVM : VMBase
    {
        private readonly IFacade facade;
        private readonly IPopupService popup;
        private readonly UserData user;
        private string oldPassword;
        private string newPassword;
        private string confirmNewPassword;
        private string message;
        private readonly RelayCommand changePassword;
        private readonly RelayCommand cancel;

        public string OldPassword
        {
            get { return oldPassword; }
            set
            {
                if (oldPassword != value)
                {
                    oldPassword = value;
                    RaisePropertyChanged("OldPassword");
                    changePassword.RaiseCanExecuteChanged();
                }
            }
        }

        public string NewPassword
        {
            get { return newPassword; }
            set
            {
                if (newPassword != value)
                {
                    newPassword = value;
                    RaisePropertyChanged("NewPassword");
                    changePassword.RaiseCanExecuteChanged();
                }
            }
        }

        public string ConfirmNewPassword
        {
            get { return confirmNewPassword; }
            set
            {
                if (confirmNewPassword != value)
                {
                    confirmNewPassword = value;
                    RaisePropertyChanged("ConfirmNewPassword");
                    changePassword.RaiseCanExecuteChanged();
                }
            }
        }

        public string Message
        {
            get { return message; }
            private set
            {
                if (message != value)
                {
                    message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }

        public ICommand ChangePassword
        {
            get { return changePassword; }
        }

        public ICommand Cancel
        {
            get { return cancel; }
        }

        public ChangePasswordVM(IFacade facade, IPopupService popup, UserData user)
        {
            this.facade = facade;
            this.popup = popup;
            this.user = user;
            this.changePassword = new RelayCommand(DoChangePassword, CanChangePassword);
            this.cancel = new RelayCommand(DoCancel);
        }

        private void DoChangePassword()
        {
            bool r;
            try
            {
                r = facade.ChangePassword(user.Username, oldPassword, newPassword);
            }
            catch (Exception ex)
            {
                facade.ShowUnexpectedError("Unable to change the password.", ex.Message);
                return;
            }

            if (r)
            {
                popup.ClosePopup();
            }
            else
            {
                Message = "Invalid old password";
                NewPassword = "";
                OldPassword = "";
            }
        }

        private bool CanChangePassword()
        {
            return !string.IsNullOrEmpty(newPassword) && newPassword == confirmNewPassword;
        }

        private void DoCancel()
        {
            popup.ClosePopup();
        }
    }
}
